// ThreadSafeRecordset.cpp: implementation of the CThreadSafeRecordset1 class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ThreadSafeRecordset1.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNAMIC(CThreadSafeRecordset1, CRecordset)

CThreadSafeRecordset1::CThreadSafeRecordset1(CDatabase* pDatabase/* = NULL*/):
CRecordset(pDatabase)
{

}

CCriticalSection CThreadSafeRecordset1::m_csProtect;

BOOL CThreadSafeRecordset1::Open(UINT nOpenType/* = AFX_DB_USE_DEFAULT_TYPE*/, LPCTSTR lpszSQL/* = NULL*/, DWORD dwOptions/* = none*/)
{
  m_csProtect.Lock();
  BOOL bRet = CRecordset::Open(nOpenType, lpszSQL, dwOptions);
  m_csProtect.Unlock();
  return bRet;
}

void CThreadSafeRecordset1::Close()
{
  m_csProtect.Lock();
  CRecordset::Close();
  m_csProtect.Unlock();
}

void CThreadSafeRecordset1::AddNew()
{
	m_csProtect.Lock();
	try
	{
		
		CRecordset::AddNew();
	}catch(CDBException e)
	{
	    TRACE1("Error %x Err Mess!\n", e.m_nRetCode, e.m_strError);
	}
	m_csProtect.Unlock();
} 

void CThreadSafeRecordset1::Delete()
{
    m_csProtect.Lock();
	try
	{
		CRecordset::Delete();
	}catch(CDBException e)
	{
		TRACE1("Error %x Err Mess!\n", e.m_nRetCode, e.m_strError);
	}
	m_csProtect.Unlock();
}

void CThreadSafeRecordset1::Edit()
{
	m_csProtect.Lock();
	
	try
	{
		CRecordset::Edit();
	}catch(CDBException e)
	{
		TRACE1("Error Edit %x Err Mess!\n", e.m_nRetCode, e.m_strError);
	}
	m_csProtect.Unlock();
}

BOOL CThreadSafeRecordset1::Update()
{
	m_csProtect.Lock();
	BOOL bRet ;
	try
	{
		bRet = CRecordset::Update();
	}catch(CDBException e)
	{
		TRACE1("Error Update %x Err Mess!\n", e.m_nRetCode, e.m_strError);
	}
	m_csProtect.Unlock();
	return bRet;
}

void CThreadSafeRecordset1::MoveNext()
{
	m_csProtect.Lock();
	try
	{
		CRecordset::MoveNext();
	}catch(CDBException e)
	{
		TRACE1("MoveNext Error %x Err Mess!\n", e.m_nRetCode, e.m_strError);
	}
	m_csProtect.Unlock();
	//return bRet;
}
void CThreadSafeRecordset1::Move(long nRows, WORD wFetchType/* = SQL_FETCH_RELATIVE*/)
{
  m_csProtect.Lock();
  CRecordset::Move(nRows, wFetchType);
  m_csProtect.Unlock();
}

BOOL CThreadSafeRecordset1::Requery()
{
  m_csProtect.Lock();
  BOOL bRet = CRecordset::Requery();
  m_csProtect.Unlock();
  return bRet;
}

BOOL CThreadSafeRecordset1::Check(RETCODE nRetCode) const
{
  m_csProtect.Lock();
  BOOL bRet = CRecordset::Check(nRetCode);
  m_csProtect.Unlock();
  return bRet;
}

BOOL CThreadSafeRecordset1::IsEOF() const
{
  m_csProtect.Lock();
  BOOL bRet = CRecordset::IsEOF();
  m_csProtect.Unlock();
  return bRet;
}

BOOL CThreadSafeRecordset1::IsBOF() const
{
  m_csProtect.Lock();
  BOOL bRet = CRecordset::IsBOF();
  m_csProtect.Unlock();
  return bRet;
}

void CThreadSafeRecordset1::CheckRowsetError(RETCODE nRetCode)
{
  m_csProtect.Lock();
  CRecordset::CheckRowsetError(nRetCode);
  m_csProtect.Unlock();
}

void CThreadSafeRecordset1::DoBulkFieldExchange(CFieldExchange* pFX)
{
  m_csProtect.Lock();
  CRecordset::DoBulkFieldExchange(pFX);
  m_csProtect.Unlock();
}

void CThreadSafeRecordset1::DoFieldExchange(CFieldExchange* pFX)
{
  m_csProtect.Lock();
  CRecordset::DoFieldExchange(pFX);
  m_csProtect.Unlock();
}

CString CThreadSafeRecordset1::GetDefaultConnect()
{
  m_csProtect.Lock();
  CString str = CRecordset::GetDefaultConnect();
  m_csProtect.Unlock();
  return str;
}

CString CThreadSafeRecordset1::GetDefaultSQL()
{
  m_csProtect.Lock();
  CString str = CRecordset::GetDefaultSQL();
  m_csProtect.Unlock();
  return str;
}

void CThreadSafeRecordset1::OnSetOptions(HSTMT hstmt)
{
  m_csProtect.Lock();
  CRecordset::OnSetOptions(hstmt);
  m_csProtect.Unlock();
}

void CThreadSafeRecordset1::SetRowsetSize(DWORD dwNewRowsetSize)
{
  m_csProtect.Lock();
  CRecordset::SetRowsetSize(dwNewRowsetSize);
  m_csProtect.Unlock();
}
